package com.zmn.oms.task.listener;

import org.apache.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.third.dj58.DaoJia58Consts;
import com.zmn.oms.third.dj58.dto.api.DaoJia58Response;
import com.zmn.oms.third.dj58.service.DaoJia58Service;
import com.zmn.vas.dubbo.consts.VasDubboConsts;
import com.zmn.vas.dubbo.interfaces.order.VasOrderManagerModifyRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * @author: HuangYongJie
 * @version: v1.0
 * @since: 2020/3/26 17:33
 **/
@Slf4j
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
public class Daojia58InsureListener {
    /**
     * 同步保单
     */
    public static final String INSURE_ACCEPT = "/insure/accept";

    /**
     * 取消保单
     */
    public static final String INSURE_CANCEL = "/insure/cancel";


    @Reference(version = VasDubboConsts.INTERFACE_VERSION, check = false)
    private VasOrderManagerModifyRemoteService vasOrderManagerModifyRemoteService;

    @Autowired
    private DaoJia58Service daoJia58Service;

    @AliyunMQHandler(tags = OmsMqTagConsts.ADOPT_DAOJIA_58_INSURE_TAGS)
    public void adoptDaojia58Insure(String message) {
        try {
            log.debug("#mq#insure#accept message:{}", message);
            DaoJia58Response res = daoJia58Service.sendRequestTo58DaoJia(INSURE_ACCEPT, message);
            if (Objects.isNull(res) || !Objects.equals(res.getCode(), DaoJia58Consts.DAOJIA58_RETURN_CODE_SUCCESS)) {
                log.error("到家58同步失败,message:{}", message);
            }
        } catch (Exception e) {
            log.error("到家58发送数据失败：{}", message, e);
            throw e;
        }
    }

    @AliyunMQHandler(tags = OmsMqTagConsts.CANCEL_DAOJIA_58_INSURE_TAGS)
    public void cancelDaojia58Insure(String message) {
        try {
            log.debug("#mq#insure#cancel message:{}", message);
            DaoJia58Response res = daoJia58Service.sendRequestTo58DaoJia(INSURE_CANCEL, message);
            if (!Objects.isNull(res) && Objects.equals(res.getCode(), DaoJia58Consts.DAOJIA58_RETURN_CODE_SUCCESS)) {
                Map<String, String> params = JSONObject.parseObject(message, new TypeReference<Map<String, String>>() {
                });
                ResponseDTO responseDTO = vasOrderManagerModifyRemoteService.refundForThird(Integer.valueOf(params.get("insureId")));
                if (!responseDTO.isSuccess()) {
                    log.error("58到家直约退款失败，error：{}", responseDTO.getMessage());
                }
            } else {
                log.error("58到家取消保单失败, message:{}", message);
            }
        } catch (Exception e) {
            log.error("到家58发送数据失败：{}", message, e);
            throw e;
        }
    }
}
